package com.jhgsys.internal.sypq.controller;


import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.authentication.ShiroHelper;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.sypq.entity.*;
import com.jhgsys.internal.sypq.service.*;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 任务管理 Controller
 *
 * @author ganjun
 * @date 2024-11-18 10:06:38
 */
@Slf4j
@Validated
@Controller
public class ProgressController extends BaseController {
     @Autowired
     private IProgressService progressService;
     @Autowired
     private  IScheduleService  scheduleService;
     @Autowired
     private ShiroHelper  shiroHelper;
    @GetMapping(FebsConstant.VIEW_PREFIX + "progress")
    @RequiresPermissions("progress:view")
    public ModelAndView ProgressIndex(@RequestParam(name="projectCode", required=false, defaultValue="") String projectCode) {
       //获取最晚完成时间
        String viewPath = jumpPage("sypq/progress/progressList");
        ModelAndView modelAndView = new ModelAndView(viewPath);
        modelAndView.addObject("projectCode", projectCode);
        AuthorizationInfo authorizationInfo = shiroHelper.getCurrentuserAuthorizationInfo();
        modelAndView.addObject("permissions", authorizationInfo.getStringPermissions());
        return  modelAndView;
    }

    @RequestMapping(value = "progress/list",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    @RequiresPermissions("progress:view")
    public List<ProgressDto>   getProgressList(TaskDto taskDto) {
        boolean isAdmin = isAdmin();
        String userid=getCurrentUser().getUserId();
       return     this.progressService.findTaskPage(taskDto,isAdmin,userid);
    }



    @Log("任务暂停")
    @PostMapping("progress/pause")
    @ResponseBody
   @RequiresPermissions("progress:pause")
    public FebsResponse pauseTask(@RequestBody TaskDto taskDto) throws FebsException {
        try {
            this.progressService.pauseTask(taskDto);
            return success();
        } catch (Exception ex) {
            return failure(ex, "修改失败");
        }
     }

    @Log("任务完成")
    @PostMapping("progress/complete")
    @ResponseBody
    @RequiresPermissions("progress:complete")
    public FebsResponse completeTask(@RequestBody TaskDto taskDto) throws FebsException {
        try {
            this.progressService.finishTask(taskDto);
            return success();
        } catch (Exception ex) {
            return failure(ex, "修改失败");
        }
    }



}
